/*
 Copyright (c) 2013, Francisco Suárez-Ruiz
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 names of its contributors may be used to endorse or promote products
 derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY Francisco Suárez <email> ''AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL Francisco Suárez <email> BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/

#include "UDPreader.h"

using namespace std;

UDPreader::UDPreader(int port_no) {
	// Construct the server sockaddr_in structure
	serverlen = sizeof(server); //size of address structures
	memset(&server, 0, serverlen); // Clear struct
	server.sin_family = AF_INET; // Internet/IP
	server.sin_addr.s_addr = htonl(INADDR_ANY); // Any IP address
	server.sin_port = htons(port_no); // server port
}

UDPreader::~UDPreader() {
}

int UDPreader::create() {
	struct timeval t_out;
	t_out.tv_sec = 0;
	t_out.tv_usec = 1000;
	// Create the UDP socket
	sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	// Set timeout
	if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &t_out, sizeof(t_out)) < 0)
		std::cout << "setsockopt [SO_RCVTIMEO] failed\n";
	return sockfd;
}

int UDPreader::bind_socket() {
	return bind(sockfd, (struct sockaddr *) &server, serverlen);
}

int UDPreader::close_socket() {
	return close(sockfd);
}

const std::vector<float> & UDPreader::receive() {
	recv_size = recvfrom(sockfd, buffer, BUFFSIZE, 0,
			(struct sockaddr *) &client, &clientlen);
	data_vector.resize(0);
	if (recv_size <= 0)
		return data_vector;
	//calculate number of doubles received and proceed
	int num_floats = (int) (recv_size / BYTES_IN_FLOAT);

	//allocate storage working number
	float extracted_num = 0;
	for (int i = 0; i < num_floats; i++) {
		//convert to float and add to the data vector
		memcpy(&extracted_num, &buffer[i*BYTES_IN_FLOAT], BYTES_IN_FLOAT);
		data_vector.push_back(extracted_num);
	}
	bzero(buffer, recv_size + 1);
	return data_vector;
}
